home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / stream.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  12KB  |  333 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* stream.h */
  20. /* Definitions for Ghostscript stream package */
  21. /* Requires stdio.h */
  22.  
  23. /*
  24.  * Note that the stream package works with bytes, not chars.
  25.  * This is to ensure unsigned representation on all systems.
  26.  * A stream currently can only be read or written, not both.
  27.  * Note also that the read procedure returns an int,
  28.  * not a char or a byte, so we can use negative values for EOFC and ERRC.
  29.  * We distinguish "data" from "signal" results (EOFC, ERRC) with a macro:
  30.  */
  31.  
  32. #define char_is_data(c) ((c) >= 0)
  33. #define char_is_signal(c) ((c) < 0)
  34. typedef struct stream_s stream;
  35.  
  36. /*
  37.  * We cast EOFC and ERRC to int explicitly, because some compilers
  38.  * don't do this if the other arm of a conditional is a byte.
  39.  * Clients should use char_is_data and char_is_signal (see above)
  40.  * to test for exceptional results.
  41.  */
  42. #define EOFC ((int)(-1))
  43. #define ERRC ((int)(-2))
  44. /****** ERRC IS NOT RECOGNIZED EVERYWHERE YET ******/
  45.  
  46. /* ------ Define the "virtual" stream procedures ------ */
  47.  
  48. typedef struct {
  49.  
  50.         /* Store # available for reading. */
  51.         /* Return 0 if OK, ERRC if error or not implemented. */
  52.     int (*available)(P2(stream *, long *));
  53.  
  54.         /* Set position. */
  55.         /* Return 0 if OK, ERRC if error or not implemented. */
  56.     int (*seek)(P2(stream *, long));
  57.  
  58.         /* Flush buffered data. */
  59.         /* Return 0 if OK, ERRC if error. */
  60.     int (*flush)(P1(stream *));
  61.  
  62.         /* Flush data (if writing) & close stream. */
  63.         /* Return 0 if OK, ERRC if error. */
  64.     int (*close)(P1(stream *));
  65.  
  66.         /* Refill buffer (setting endptr) and reset cptr. */
  67.         /* Return ERRC if not implemented; */
  68.         /* otherwise, set end_status appropriately and return 0. */
  69.     int (*read_buf)(P1(stream *));
  70.  
  71.         /* Write buffer and reset cptr. */
  72.         /* Return 0 if OK, ERRC if error or not implemented. */
  73.     int (*write_buf)(P1(stream *));
  74.  
  75. } stream_procs;
  76.  
  77. /* ------ Structs for specialized streams -- see below. ------ */
  78.  
  79. typedef struct CCITTFax_state_s {
  80.     /* The following are set before initialization. */
  81.     int Uncompressed;    /* boolean */
  82.     int K;
  83.     int EndOfLine;        /* boolean */
  84.     int EncodedByteAlign;    /* boolean */
  85.     int Columns;
  86.     int Rows;
  87.     int EndOfBlock;        /* boolean */
  88.     int BlackIs1;        /* boolean */
  89.     int DamagedRowsBeforeError;
  90.     int FirstBitLowOrder;    /* boolean */
  91.     uint raster;
  92.     /* The following are updated dynamically. */
  93.     int k_left;        /* number of next rows to encode in 2-D */
  94.                 /* (only if K > 0) */
  95.     int cbit;        /* bits left to fill in current decoded */
  96.                 /* byte at *cptr (0..7, input only) */
  97.     uint prev_pos;        /* position of previous line in buffer */
  98.     int rows_left;        /* number of rows left (input only) */
  99.     /* The following are input-only and not used yet. */
  100.     int uncomp_run;        /* non-0 iff we are in an uncompressed */
  101.                 /* run straddling a scan line (-1 if white, */
  102.                 /* 1 if black) */
  103.     int uncomp_left;    /* # of bits left in the run */
  104.     int uncomp_exit;    /* non-0 iff this is an exit run */
  105.                 /* (-1 if next run white, 1 if black) */
  106. } CCITTFax_state;
  107.  
  108. /****** STUB ******/
  109. struct dct_color_params_s;
  110. typedef struct DCT_state_s {
  111.     /* The following are set before initialization. */
  112.     int Columns;
  113.     int Rows;
  114.     int Colors;
  115.     float QFactor;
  116.     struct dct_color_params_s *params;    /* params[Colors] */
  117.     int ColorTransform;    /* 0 or 1 */    
  118. } DCT_state;
  119.  
  120. struct lzw_decode_table_s;
  121. struct lzw_encode_table_s;
  122. typedef struct LZW_state_s {
  123.     /* The following are set at initialization. */
  124.     int enhanced;            /* if true, use Aladdin's */
  125.                     /* enhanced compression algorithm */
  126.     /* The following are updated dynamically. */
  127.     struct lzw_decode_table_s *decode_table;    /* decoding table */
  128.     struct lzw_encode_table_s *encode_table;    /* encoding table */
  129.     uint next_code;            /* next code to be assigned */
  130.     int code_size;            /* current # of bits per code */
  131.     int prev_code;            /* previous code recognized */
  132.                     /* or assigned */
  133. } LZW_state;
  134.  
  135. typedef struct SubFile_state_s {
  136.     ulong count;        /* # of EODs to scan over */
  137.     const byte *eod_string;
  138.     uint string_size;
  139.     uint match;        /* # of matched chars preceding end of buffer */
  140. } SubFile_state;
  141.  
  142. /* ------ The actual stream structure ------ */
  143.  
  144. struct file_device_s;
  145.  
  146. struct stream_s {
  147.     byte *cptr;            /* pointer to last byte */
  148.                     /* read or written */
  149.     byte *endptr;            /* pointer to last byte */
  150.                     /* containing data for reading, */
  151.                     /* or to be filled for writing */
  152.     byte *cbuf;            /* base of buffer */
  153.     uint bsize;            /* size of buffer, 0 if closed */
  154.     uint cbsize;            /* size of buffer */
  155.     uint modes;            /* (not byte, to keep alignment) */
  156. #define s_mode_read 1
  157. #define s_mode_write 2
  158. #define s_mode_seek 4
  159. #define s_mode_append 8            /* (s_mode_write also set) */
  160. #define s_is_valid(s) ((s)->modes != 0)
  161. #define s_is_reading(s) (((s)->modes & s_mode_read) != 0)
  162. #define s_is_writing(s) (((s)->modes & s_mode_write) != 0)
  163. #define s_can_seek(s) (((s)->modes & s_mode_seek) != 0)
  164.     int end_status;            /* EOFC if at EOF when buffer */
  165.                     /* becomes empty, ERRC if error */
  166.     long position;            /* file position of beginning of */
  167.                     /* buffer */
  168.     stream_procs procs;
  169.     int num_format;            /* format for Level 2 */
  170.                     /* encoded number reader */
  171.                     /* (only used locally) */
  172.     stream *strm;            /* the underlying stream, non-zero */
  173.                     /* iff this is a filter stream */
  174.     int strm_is_temp;        /* if true, strm is a temporary */
  175.                     /* stream and should be freed */
  176.                     /* when this stream is closed */
  177.     ushort read_id;            /* "unique" serial # for detecting */
  178.                     /* references to closed streams */
  179.                     /* and for validating read access */
  180.     ushort write_id;        /* ditto to validate write access */
  181.     int inline_temp;        /* temporary for inline access */
  182.                     /* (see spgetc_inline below) */
  183.     /*
  184.      * If were were able to program in a real object-oriented style, 
  185.      * the remaining data would be per-subclass.  It's just too much
  186.      * of a nuisance to do this in C, so we allocate space for the
  187.      * private data of ALL subclasses.
  188.      */
  189.     /* The following are for file streams. */
  190.     struct file_device_s *fdev;    /* the file device */
  191.     FILE *file;            /* file handle for C library */
  192.     int (*save_close)(P1(stream *));    /* save original close proc */
  193.     stream *prev, *next;        /* keep track of all files */
  194.     /*
  195.      * The remaining members are only for filters.
  196.      * We simply allocate space for the simple ones,
  197.      * and only bother with a union for the complex ones.
  198.      */
  199.     /* The following is used by several decoding filters. */
  200.     int odd;            /* odd digit */
  201.     /* The following is for RunLengthEncode. */
  202.     ulong record_size;
  203.     /* The following is for RunLengthEncode and PFBDecode. */
  204.     ulong record_left;        /* bytes left in current record */
  205.     /* The following are for PFBDecode. */
  206.     int record_type;
  207.     int binary_to_hex;
  208.     /* The following are for eexecDecode. */
  209.     ushort cstate;            /* encryption state */
  210.     ushort _skip;            /* (keep int alignment) */
  211.     int binary;            /* true=binary, false=hex */
  212.     /* The following are for bit-oriented filters */
  213.     /* (see sbits.h for more details.) */
  214.     uint bits;        /* most recent bits of input or */
  215.                 /* current bits of output */
  216.     int bits_left;        /* # of valid low bits (input) or */
  217.                 /* unused low bits (output) in above */
  218.     int reverse_bits;    /* if true, reverse bit order */
  219.     /* The following are for various filters. */
  220.     union {
  221.         CCITTFax_state cf;
  222.         DCT_state dct;
  223.         LZW_state lzw;
  224.         SubFile_state sf;
  225.     } state;
  226. };
  227.  
  228. /* Initialize the checking IDs of a stream. */
  229. #define s_init_ids(s) ((s)->read_id = (s)->write_id = 1)
  230. #define s_init_read_id(s) ((s)->read_id = 1, (s)->write_id = 0)
  231. #define s_init_write_id(s) ((s)->read_id = 0, (s)->write_id = 1)
  232. #define s_init_no_id(s) ((s)->read_id = (s)->write_id = 0)
  233.  
  234. /* ------ Stream functions ------ */
  235.  
  236. /* Some of these are macros -- beware. */
  237. /* Note that unlike the C stream library, */
  238. /* ALL stream procedures take the stream as the first argument. */
  239. #define sendbufp(s) ((s)->cptr >= (s)->endptr)    /* not for clients */
  240.  
  241. /* Following are valid for all streams. */
  242. /* flush is NOT a no-op for read streams -- it discards data until EOF. */
  243. /* close is NOT a no-op for non-file streams -- */
  244. /* it actively disables them. */
  245. /* The close routine must do a flush if needed. */
  246. #define sseekable(s) s_can_seek(s)
  247. #define serrorp(s) (sendbufp(s) && (s)->end_status == ERRC)
  248. #define savailable(s,pl) (*(s)->procs.available)(s,pl)
  249. #define sflush(s) (*(s)->procs.flush)(s)
  250. extern int sclose(P1(stream *));
  251.  
  252. /* Following are only valid for read streams. */
  253. extern int spgetc(P1(stream *));
  254. /* The first alternative should read */
  255. /*    (int)(*++((s)->cptr))    */
  256. /* but the Borland compiler generates truly atrocious code for this. */
  257. /* The SCO ODT compiler requires the first, pointless cast to int. */
  258. #define sgetc(s)\
  259.   ((int)(!sendbufp(s) ? (++((s)->cptr), (int)*(s)->cptr) : spgetc(s)))
  260. extern uint sgets(P3(stream *, byte *, uint));
  261. extern int sreadhex(P6(stream *, byte *, uint, uint *, int *, int));
  262. extern int sungetc(P2(stream *, byte));    /* ERRC on error, 0 if OK */
  263. #define sputback(s) ((s)->cptr--)    /* can only do this once! */
  264. #define seofp(s) (sendbufp(s) && (s)->end_status == EOFC)
  265. extern int spskip(P2(stream *, long));
  266. #define sskip(s,n) spskip(s,(long)(n))
  267.  
  268. /* Following are only valid for write streams. */
  269. extern int spputc(P2(stream *, byte));
  270. /* The first alternative should read */
  271. /*    ((int)(*++((s)->cptr)=(c)))    */
  272. /* but the Borland compiler generates truly atrocious code for this. */
  273. #define sputc(s,c)\
  274.   (!sendbufp(s) ? (++((s)->cptr), *(s)->cptr=(c), 0) : spputc((s),(c)))
  275. extern uint sputs(P3(stream *, const byte *, uint));
  276.  
  277. /* Following are only valid for positionable streams. */
  278. #define stell(s) ((s)->cptr + 1 - (s)->cbuf + (s)->position)
  279. #define sseek(s,pos) (*(s)->procs.seek)(s,(long)(pos))
  280.  
  281. /* Following are for high-performance clients. */
  282. /* bufptr points to the next item, bufend points beyond the last item. */
  283. #define sbufptr(s) ((s)->cptr + 1)
  284. #define sbufend(s) ((s)->endptr + 1)
  285. #define ssetbufptr(s,ptr) ((s)->cptr = (ptr) - 1)
  286. #define sbufskip(s,n) ((s)->cptr += (n))
  287. #define sbufavailable(s) ((s)->endptr - (s)->cptr)
  288.  
  289. /* The following are for very high-performance clients of read streams, */
  290. /* who unpack the stream state into local variables. */
  291. /* Note that any non-inline operations must do a s_end_inline before, */
  292. /* and a s_begin_inline after. */
  293. #define s_declare_inline(s, cp, ep)\
  294.   register byte *cp;\
  295.   byte *ep
  296. #define s_begin_inline(s, cp, ep)\
  297.   cp = (s)->cptr, ep = (s)->endptr
  298. #define s_end_inline(s, cp, ep)\
  299.   (s)->cptr = cp
  300. #define sbufavailable_inline(s, cp, ep)\
  301.   (ep - cp)
  302. #define sendbufp_inline(s, cp, ep)\
  303.   (cp >= ep)
  304. /* The (int) is needed to pacify the SCO ODT compiler. */
  305. #define sgetc_inline(s, cp, ep)\
  306.   ((int)(sendbufp_inline(s, cp, ep) ? spgetc_inline(s, cp, ep) : *++cp))
  307. #define spgetc_inline(s, cp, ep)\
  308.   (s_end_inline(s, cp, ep), (s)->inline_temp = spgetc(s),\
  309.    s_begin_inline(s, cp, ep), (s)->inline_temp)
  310. #define sputback_inline(s, cp, ep)\
  311.   --cp
  312.  
  313. /* Stream creation procedures */
  314. extern    void    sread_string(P3(stream *, const byte *, uint)),
  315.         swrite_string(P3(stream *, byte *, uint));
  316. extern    void    sread_file(P4(stream *, FILE *, byte *, uint)),
  317.         swrite_file(P4(stream *, FILE *, byte *, uint)),
  318.         sappend_file(P4(stream *, FILE *, byte *, uint));
  319.  
  320. /* Standard stream initialization */
  321. extern    void    s_std_init(P5(stream *, byte *, uint, const stream_procs *, int /*mode*/));
  322.  
  323. /* Standard stream finalization */
  324. extern    void    s_disable(P1(stream *));
  325.  
  326. /* Generic stream procedures exported for filters */
  327. extern    int    s_std_null(P1(stream *)),
  328.         s_std_read_flush(P1(stream *)),
  329.         s_std_write_flush(P1(stream *)),
  330.         s_std_noavailable(P2(stream *, long *)),
  331.         s_std_noseek(P2(stream *, long));
  332. #define s_std_close s_std_null
  333.